package com.boshao.classschedule.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.boshao.classschedule.ClassApplication;
import com.boshao.classschedule.R;
import com.boshao.classschedule.adapter.MainFragmentAdapter;
import com.boshao.classschedule.widget.ActionBarLeft;

/**
 * 主界面Fragment
 * @author boshao
 *
 */
public class MainFragment extends AbstartFragment implements OnClickListener{
	public static final String TAG = "main_fragment";
	private View mRootView;
	private ViewPager mViewPager;
	private LinearLayout mIndicator;
	private TextView[] mIndicators = new TextView[3];
	private FragmentManager mFragmentManager;
	public static final int CALENDAR = 0;
	public static final int CLASS = 1;
	
	public static AbstartFragment newInstance(){
		MainFragment fragment = new MainFragment();
		return fragment;
	}
	
	@Override
	protected ActionBarLeft getActionBarLeft() {
		return ActionBarLeft.NULL;
	}
	
	@Override
	protected void onClickActionRight(int resId) {
		switch (resId) {
		case R.id.action_right_one://添加图标时间监听
			if(ClassApplication.getInstance().getAddWhat() == CALENDAR){
				addTimeManager();				
			}else if(ClassApplication.getInstance().getAddWhat() == CLASS){
				addClassSchedule();
			}
			break;
		case R.id.action_right_two://搜索监听
			onSearch();
			break;
		case R.id.actionbar_right_three:
			break;	
		}
	}
	
	private void onSearch() {
		AbstartFragment fragment = SearchFragment.newInstance();
		getMainActivity().addFragment(mFragmentManager,fragment,fragment.getTag(),true);
	}

	private void addClassSchedule() {
		AbstartFragment fragment = AddOrModifyClassFragment.newInstance(mViewPager);
		getMainActivity().addFragment(mFragmentManager,fragment,fragment.getTag(),true);
	}

	private void addTimeManager() {
		AbstartFragment fragment = TimeManagerFragment.newInstance(mViewPager);
		getMainActivity().addFragment(mFragmentManager,fragment,fragment.getTag(),true);
	}

	@Override
	protected int getContentLayoutId() {
		return R.id.fragment_main;
	}
	
	@Override
	protected String getFragmentTag() {
		return TAG;
	}
	
	//创建view
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mFragmentManager = getActivity().getSupportFragmentManager();
		View view = super.onCreateView(inflater, container, savedInstanceState);
		initView(inflater, container, view);
		return view;
	}

	//初始化view
	private void initView(LayoutInflater inflater, ViewGroup container,
			View view) {
		mIndicator = (LinearLayout) inflater.inflate(R.layout.indexindicator,container,false);
		mIndicators[0] = (TextView) mIndicator.findViewById(R.id.index_1);
		mIndicators[1] = (TextView) mIndicator.findViewById(R.id.index_2);
		mIndicators[2] = (TextView) mIndicator.findViewById(R.id.index_3);
		for(int i = 0;i < mIndicators.length;i++){
			mIndicators[i].setOnClickListener(this);
		}
		RelativeLayout rlActionbar = (RelativeLayout) view.findViewById(R.id.rl_actionbar);
		rlActionbar.addView(mIndicator);
		RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mIndicator.getLayoutParams();
		layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		mViewPager.setOffscreenPageLimit(3);
		mViewPager.setAdapter(new MainFragmentAdapter(getActivity().getSupportFragmentManager(),mIndicators.length));
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				mIndicators[position].setTextColor(getResources().getColor(R.color.white));
				for(int i = 0;i < mIndicators.length;i++){
					if(i != position){
						mIndicators[i].setTextColor(getResources().getColor(R.color.gray));
					}
				}
				if(position == 0){
					ClassApplication.getInstance().setAddWhat(CALENDAR);
				}else if(position == 1){
					ClassApplication.getInstance().setAddWhat(CLASS);
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		mViewPager.setCurrentItem(0);
		mIndicators[0].setTextColor(getResources().getColor(R.color.white));
		ClassApplication.getInstance().setAddWhat(CALENDAR);
	}
	
	@Override
	protected View onAppCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mRootView =  inflater.inflate(R.layout.fragment_main,container,false);
		mViewPager = (ViewPager) mRootView.findViewById(R.id.vp);
		return mRootView;
	}

	@Override
	public void onClick(View v) {//点击左上角,日历、课程、其他的监听
		switch(v.getId()){
			case R.id.index_1:
				mViewPager.setCurrentItem(0);
				break;
			case R.id.index_2:
				mViewPager.setCurrentItem(1);
				break;
			case R.id.index_3:
				mViewPager.setCurrentItem(2);
				break;
		}
	}
	
	protected int getAnimationStyle(){
		return NO_ANIMATION;
	}
}
